Fx.Parallax = new Class({

	Implements: [Events, Options],

	options: {
		limit: {w:100, h:100},
		styles: {x: 'margin-left', y:'top'}
	},
	
	initialize: function(options){
		this.setOptions(options)
		this.pObjects = [];
		//setup mousemove event
		document.addEvent('mousemove', this.update.bind(this));
	},
	
	update: function(e){
		var p = this.getPercent(e.client);
		
		this.pObjects.each(function(el,i){
			this.setPosition(el,(this.pObjects.length-i), p);
		}, this);
	},
	
	add: function(elements, options){
		//adds elements to a level in the parallax or a new level if level is undefined
		elements = $$(elements);
		if(!options){options = {}};
		elements.each(function(el,i){
			el.store('x', el.getStyle(this.options.styles.x));
			el.store('y', el.getStyle(this.options.styles.y));
			el.store('options', options);
			el.store('styles', this.options.styles);
		}, this);
		
		if(options.level != undefined){
			this.pOjbects[options.level].combine(elements);
		}else{
			this.pObjects.push(elements);
		}
	},
	
	remove: function(element, level){
		if(element){
			this.pObjects[level].erase(element);
		}else{
			this.pObjects.splice(level, 1);
		}
	},
	
	getPercent: function(client){
		//calculates the percent of the parallax
		
		var percent = {x:0, y:0};
		
		var w = window.getWidth();
		var h = window.getHeight();
		
		if(client.y < (h/2)){
			var p = Math.round((client.y/(h/2)) * 100);
			p = 100-p;
			percent.y  = Math.round((this.options.limit.h/100)*p);
		}
		if(client.y > (h/2)){
			var p = Math.round((client.y/(h/2)) * 100);
			p = 100-p;
			percent.y  = Math.round((this.options.limit.h/100)*p);
		}
		if(client.x < (w/2)){
			var p = Math.round((client.x/(w/2)) * 100);
			p = 100-p;
			percent.x  = Math.round((this.options.limit.w/100)*p);
		}
		if(client.x > (w/2)){
			var p = Math.round((client.x/(w/2)) * 100);
			p = 100-p;
			percent.x  = Math.round((this.options.limit.w/100)*p);
		}
		
		return percent;
	},
	
	setPosition: function(element, amount, percent){
		//sets the position of the element with the parallax amount applied
		var opts = element.retrieve('options')[0];
		amount += 4;
		var o = {};
		if(opts.x != false){
			o[this.options.styles.x] = element.retrieve('x')[0].toInt()+(percent.x/amount);
		}
		if(opts.y != false){
			o[this.options.styles.y] = element.retrieve('y')[0].toInt()+(percent.y/amount);
		}
		element.setStyles(o);
	}
});


Fx.Morph = new Class({

	Extends: Fx.Morph,

	set: function(now){
		if (typeof now == 'string') now = this.search(now);
		for (var p in now) {
			var s = this.element.retrieve('styles');
			if(p == s.x){
				this.element.store('x', now[p][0].value);
			}else if(p == s.y){
				this.element.store('y', now[p][0].value);
			}
			this.render(this.element, p, now[p], this.options.unit);
		}
		return this;
	}

});
